package com.loukou.lkt.lkt.dao;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.transaction.annotation.Transactional;

import com.loukou.lkt.lkt.entity.PoPurchaseOrderDetail;

public interface PoPurchaseOrderDetailDao extends PagingAndSortingRepository<PoPurchaseOrderDetail, Integer>{

	List<PoPurchaseOrderDetail> findByTaosku(String taosku);

	List<PoPurchaseOrderDetail> findByOrderId(int orderId);
	
	@Modifying
	@Transactional
	@Query("update PoPurchaseOrderDetail set receiveNumber=?2 where id=?1")
	void updateReceiveNumberForApi(int id,int receiveNumber);
	
	@Query("SELECT s FROM PoPurchaseOrderDetail s WHERE taosku = ?1 AND receiveNumber > 0")
	Page<PoPurchaseOrderDetail> findByTaosku(String taosku, Pageable pageable);

	@Modifying
	@Transactional
	@Query("update PoPurchaseOrderDetail set taxPrice=?2, discountRate = ?3, orderNumber=?4, taxAmount=?5 where id=?1")
	void updateDetailForEdit(int id, double purchasePrice, double discountRate,
			int purchaseNum, double goodsAmount);

	List<PoPurchaseOrderDetail> findByOrderIdIn(List<Integer> orderIds);

	@Query(value = "select  * from lkt_po_purchaseorder_detail  where goods_id = ?1 and receive_number > 0 "
			+ "and tax_price > 0 order by id desc limit 1", nativeQuery = true)
	PoPurchaseOrderDetail findOneByGoodsId(int goodsId);

	Page<PoPurchaseOrderDetail> findByOrderIdIn(List<Integer> detailIds, Pageable pageable);

}
